﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace PatientInformationExtract.Dictionary.Views
{
    /// <summary>
    /// Interaction logic for SynonymListViewItem.xaml
    /// </summary>
    public partial class SynonymListViewItem : UserControl
    {
        public delegate void SynonymListViewItemDeletedHandler(SynonymListViewItem item);
        public event SynonymListViewItemDeletedHandler SynonymListViewItemDeletedEvent;

        public string Text
        {
            get
            {
                return txt.Text;
            }
            set
            {
                txt.Text = value;
            }
        }

        private bool _isEnabled;
        new public bool IsEnabled
        {
            get
            {
                return this._isEnabled;
            }
            set
            {
                txt.IsEnabled = value;
                btnDelete.IsEnabled = value;
                this._isEnabled = value;
            }
        }

        public SynonymListViewItem()
        {
            InitializeComponent();
            _isEnabled = false;
            IsEnabled = _isEnabled;
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(SynonymListViewItem_IsVisibleChanged);
        }

        private void SynonymListViewItem_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                Dispatcher.BeginInvoke(
                DispatcherPriority.ContextIdle,
                new Action(delegate()
                {
                    txt.Focus();
                }));
            } 
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (SynonymListViewItemDeletedEvent != null)
            {
                this.SynonymListViewItemDeletedEvent(this);
            }
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txt.Text == "")
            {
                if (SynonymListViewItemDeletedEvent != null)
                {
                    this.SynonymListViewItemDeletedEvent(this);
                }
            }
        }
    }
}
